import { defineStore } from 'pinia'
import { shallowRef } from 'vue'
import type WebsiteInfo from '@/api/website/type.ts'
import { getWebsiteInfo } from '@/api/website'

const useWebsiteStore = defineStore('website', () => {
  const webInfo = shallowRef<WebsiteInfo>()

  // 获取网站信息
  const getInfo = async () => {
    const res = await getWebsiteInfo()
    // 获取当前时间的时间戳
    const now = new Date().getTime()
    // 将字符串转换为日期对象
    const lastUpdated = new Date(res.data.lastUpdateTime)
    // 获取最后更新时间的时间戳
    const lastUpdatedTimestamp = lastUpdated.getTime()
    // 计算日期差异
    const differenceInDays = Math.floor((now - lastUpdatedTimestamp) / (1000 * 60 * 60 * 24))
    res.data.lastUpdateTime = differenceInDays + '天前'
    webInfo.value = res.data
  }

  return {
    webInfo,
    getInfo
  }
})

export default useWebsiteStore
